import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/tooltip/gt_tooltip_widget_v3.dart';

class GTTooltipV3 extends StatefulWidget {
  //目标控件
  final Widget child;

  //气泡相对目标控件的展示位置
  final GTToolTipPositionV3 position;

  //气泡文字
  final String tooltipText;

  //是否展示关闭按钮
  final bool? showClose;

  //关闭按钮回调
  final VoidCallback? onCloseClick;

  //点击外部dialog是否消失
  final bool? clickMaskDismiss;

  //多少时间后自动消失
  final Duration? autoHiddenDuration;

  //屏幕上交互事件是否可以穿透遮罩背景，默认false不可以
  final bool? usePenetrate;

  //是否需要点击展示气泡；默认直接展示
  final bool? needClick;

  //目标控件点击事件
  final VoidCallback? onChildClick;

  const GTTooltipV3(
      {super.key,
      required this.child,
      required this.position,
      required this.tooltipText,
      this.showClose,
      this.onCloseClick,
      this.clickMaskDismiss = true,
      this.autoHiddenDuration,
      this.usePenetrate = false,
      this.needClick = false,
      this.onChildClick});

  @override
  State<StatefulWidget> createState() => _TooltipState();
}

class _TooltipState extends State<GTTooltipV3> {
  final GlobalKey _globalKey = GlobalKey();

  @override
  void initState() {
    if (widget.needClick ?? false) {
      return;
    }
    if (widget.autoHiddenDuration != null) {
      Future.delayed(widget.autoHiddenDuration!).then((value) {
        if (mounted) {
          SmartDialog.dismiss(tag: widget.tooltipText);
          if (widget.onCloseClick != null) {
            widget.onCloseClick!();
          }
        }
      });
    }

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      Future.delayed(const Duration(milliseconds: 350)).then((value) {
        if (mounted && _globalKey.currentContext != null) {
          showTooltip(_globalKey.currentContext!, widget.position, widget.tooltipText,
              showClose: widget.showClose,
              clickMaskDismiss: widget.clickMaskDismiss,
              onCloseClick: widget.onCloseClick);
        }
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (widget.needClick ?? true) {
          if (isShowing) {
            isShowing = false;
            SmartDialog.dismiss(tag: widget.tooltipText);
            return;
          }
          showTooltip(_globalKey.currentContext!, widget.position, widget.tooltipText,
              showClose: widget.showClose,
              clickMaskDismiss: widget.clickMaskDismiss,
              onCloseClick: widget.onCloseClick);
        }
        if (widget.onChildClick != null) widget.onChildClick!();
      },
      child: SizedBox(
        key: _globalKey,
        child: widget.child,
      ),
    );
  }

  bool isShowing = false;

  void showTooltip(BuildContext context, GTToolTipPositionV3 position, String tooltipText,
      {bool? showClose = false, VoidCallback? onCloseClick, bool? clickMaskDismiss = true}) {
    RenderBox renderBox = context.findRenderObject() as RenderBox;
    double targetWidth = renderBox.size.width;
    Alignment alignment;
    bool isRTL = GTThemeProvider.instance.isRTL();
    //气泡弹窗位置
    switch (position) {
      case GTToolTipPositionV3.topCenter:
        alignment = Alignment.topCenter;
        break;
      case GTToolTipPositionV3.topStart:
        alignment = Alignment.topLeft;
        if (isRTL) {
          alignment = Alignment.topRight;
        }
        break;
      case GTToolTipPositionV3.topEnd:
        alignment = Alignment.topRight;
        if (isRTL) {
          alignment = Alignment.topLeft;
        }
        break;
      case GTToolTipPositionV3.bottomCenter:
        alignment = Alignment.bottomCenter;
        break;
      case GTToolTipPositionV3.bottomStart:
        alignment = Alignment.bottomLeft;
        if (isRTL) {
          alignment = Alignment.bottomRight;
        }
        break;
      case GTToolTipPositionV3.bottomEnd:
        alignment = Alignment.bottomRight;
        if (isRTL) {
          alignment = Alignment.bottomLeft;
        }
        break;
      case GTToolTipPositionV3.startCenter:
        alignment = Alignment.centerLeft;
        if (isRTL) {
          alignment = Alignment.centerRight;
        }
        break;
      case GTToolTipPositionV3.startTop:
        alignment = Alignment.centerLeft;
        if (isRTL) {
          alignment = Alignment.centerRight;
        }
        break;
      case GTToolTipPositionV3.startBottom:
        alignment = Alignment.centerLeft;
        if (isRTL) {
          alignment = Alignment.centerRight;
        }
        break;
      case GTToolTipPositionV3.endCenter:
        alignment = Alignment.centerRight;
        if (isRTL) {
          alignment = Alignment.centerLeft;
        }
        break;
      case GTToolTipPositionV3.endTop:
        alignment = Alignment.centerRight;
        if (isRTL) {
          alignment = Alignment.centerLeft;
        }
        break;
      case GTToolTipPositionV3.endBottom:
        alignment = Alignment.centerRight;
        if (isRTL) {
          alignment = Alignment.centerLeft;
        }
        break;
    }
    SmartDialog.config.attach = SmartConfigAttach(
        attachAlignmentType: SmartAttachAlignmentType.inside,
        clickMaskDismiss: clickMaskDismiss ?? true,
        usePenetrate: widget.usePenetrate ?? false);
    isShowing = true;
    SmartDialog.showAttach(
      maskColor: Colors.transparent,
      targetContext: context,
      alignment: alignment,
      tag: tooltipText,
      bindWidget: context,
      builder: (context) {
        return GTTooltipWidgetV3(
          text: tooltipText,
          position: position,
          targetWidth: targetWidth,
          showClose: showClose,
          onCloseClick: () {
            SmartDialog.dismiss(tag: tooltipText);
            if (onCloseClick != null) {
              onCloseClick();
            }
          },
        );
      },
      onDismiss: () {
        isShowing = false;
      },
    );
  }
}
